package a6polo.net.tencen.qzone;

import java.net.URLEncoder;


import net.a6polo.renren.UTF8PostMethod;
import net.a6polo.renren.domain.PhotosUploadResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import com.google.gson.Gson;

import a6polo.net.tencen.qzone.domain.AddShareResponse;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class QzoneApi {

	public static final String clientId = "100355153";
	public static final String clientSecret = "c92c4797137cc4d90b89c457a6b64ab7";
	public static final String redirectUri = "auth://tauth.qq.com/";
	public static String scope = "get_user_info,get_user_profile,add_share,add_topic,list_album,upload_pic,add_album";// 授权范围

	private String token;
	private String openid;

	public QzoneApi(Context ctx) {
		SharedPreferences sp = ctx.getSharedPreferences("qq_token", ctx.MODE_PRIVATE);
		token = sp.getString("accessToken", "none");
		openid = sp.getString("openId", "none");
		
	}

	public String getOpenid(String accessToken) {
		String openid = null;
		String response = null;
		String url = "https://graph.z.qq.com/moc2/me?access_token=" + accessToken;
		GetMethod method = new GetMethod(url);

		HttpClient client = new HttpClient();
		try {
			client.executeMethod(method);
			response = method.getResponseBodyAsString(); // 返回请求的结果
			Log.d("a6polo", "----------------Openid response:" + response);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (null != response && response.indexOf("openid") != -1) {
			int startPos = response.indexOf("openid=") + 7;
			openid = response.substring(startPos);
		}
		return openid;
	}
	
	public AddShareResponse addShare(String title, String url, String site, String fromurl, String comment) {
		String apiUrl = "https://graph.qq.com/share/add_share";
		String json = null;
		
		title = URLEncoder.encode(title);
		url = URLEncoder.encode(url);
		site = URLEncoder.encode(site);
		fromurl = URLEncoder.encode(fromurl);
		
		UTF8PostMethod method = new UTF8PostMethod(apiUrl);
		// 将以上准备好的参数添加到method对象中
		method.addParameter("access_token", token);
		method.addParameter("oauth_consumer_key", clientId);  //appid
		method.addParameter("openid", openid);
		method.addParameter("title", title);
		method.addParameter("url", "JSON"); // 返回结果的形式，支持XML或者JSON两种形式，默认为XML
		method.addParameter("site", site);
		method.addParameter("fromurl", fromurl);
		method.addParameter("comment", comment);
		method.addParameter("format", "json");
		
		HttpClient client = new HttpClient();
		try {
			client.executeMethod(method);
			json = method.getResponseBodyAsString(); // 返回请求的结果
			Log.d("a6polo", "addShare result:" + json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
		Gson gson = new Gson();
		return gson.fromJson(json, AddShareResponse.class);
	}
	
	public AddShareResponse addShare(String title, String url, String site, String fromurl) {
		String apiUrl = "https://graph.qq.com/share/add_share";
		String json = null;
		
		title = URLEncoder.encode(title);
		url = URLEncoder.encode(url);
		site = URLEncoder.encode(site);
		fromurl = URLEncoder.encode(fromurl);
		
		UTF8PostMethod method = new UTF8PostMethod(apiUrl);
		// 将以上准备好的参数添加到method对象中
		method.addParameter("access_token", token);
		method.addParameter("oauth_consumer_key", clientId);  //appid
		method.addParameter("openid", openid);
		method.addParameter("title", title);
		method.addParameter("url", "JSON"); // 返回结果的形式，支持XML或者JSON两种形式，默认为XML
		method.addParameter("site", site);
		method.addParameter("fromurl", fromurl);
		method.addParameter("format", "json");
		
		HttpClient client = new HttpClient();
		try {
			client.executeMethod(method);
			json = method.getResponseBodyAsString(); // 返回请求的结果
			Log.d("a6polo", "addShare result:" + json);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 
		Gson gson = new Gson();
		return gson.fromJson(json, AddShareResponse.class);
	}
}
